//
//  SportTargetViewController.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/22.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "SportTargetViewController.h"
#import <UICountingLabel/UICountingLabel.h>
#import "ASValueTrackingSlider.h"
#import "ASProgressPopUpView.h"


#define TotalProgressWidth self.slider.frame.size.width
#define TotalSwingTarget (4000)
#define UnitSpace (100/((TotalSwingTarget - 50.0)/50.0))

@interface SportTargetViewController ()<ASValueTrackingSliderDataSource>
@property (weak, nonatomic) IBOutlet UICountingLabel *weekTargetSwingLabel;

@property (weak, nonatomic) IBOutlet ASValueTrackingSlider *slider;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *averageLineValueConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *recommendLineValueConstraint;
@property (weak, nonatomic) IBOutlet UICountingLabel *totalSportTimeLabel;
@property (weak, nonatomic) IBOutlet UICountingLabel *totalCaloriosLabel;
@property (assign, nonatomic) NSInteger currentTargetSwings;//滑动条当前的目标拍数
@property (strong, nonatomic) UIView *coverView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descViewConH;
    @property (weak, nonatomic) IBOutlet UILabel *targetDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *weekTargetDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *averageLabel;
@property (weak, nonatomic) IBOutlet UILabel *recommendLabel;
@property (weak, nonatomic) IBOutlet UILabel *recreationalLabel;
@property (weak, nonatomic) IBOutlet UILabel *healthyLabel;
@property (weak, nonatomic) IBOutlet UILabel *diligentLabel;
@property (weak, nonatomic) IBOutlet UILabel *crazyLabel;
@property (weak, nonatomic) IBOutlet UILabel *targetFinishLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalExerciseDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalCaloriesDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *minuteUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *kcalUnitLabel;

@end

@implementation SportTargetViewController

#pragma mark - ViewLifeCircle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureLocalization];
    [self configureNav];
    [self configureProgressView];
    [self configureCountingLabel];
    [self configureHasSettingTargetSwings];
}

#pragma mark - Localization

- (void)configureLocalization
{
    YFLanguage currentLanguage = [NSBundle currentLangeuage];
    if (currentLanguage == kLanguageChineseSimple) {
        self.descViewConH.constant = 60.0f;
    }else if(currentLanguage == kLanguageFrennch){
        self.descViewConH.constant = 80.0f;
        if (kYFScreenWidth < 375) {
          self.weekTargetSwingLabel.font = [UIFont systemFontOfSize:30];
          self.targetDescriptionLabel.font = [UIFont systemFontOfSize:11];
        }
    }else {
        self.descViewConH.constant = 75.0f;
    }
    
    self.title = NSLocalizedString(@"Weekly Target Setting", nil);
    self.targetDescriptionLabel.text = NSLocalizedString(@"The average value is obtained from average exercise data processing of the users, while the recommend value is derived from your personal information combined with past performance.", nil);
    self.weekTargetDescriptionLabel.text = NSLocalizedString(@"Week Target", nil);
    self.averageLabel.text = NSLocalizedString(@"Average", nil);
    self.recommendLabel.text = NSLocalizedString(@"Recommendation", nil);
    self.recreationalLabel.text = NSLocalizedString(@"Recreational", nil);
    self.healthyLabel.text = NSLocalizedString(@"Healthy", nil);
    self.diligentLabel.text = NSLocalizedString(@"Diligent", nil);
    self.crazyLabel.text = NSLocalizedString(@"Crazy", nil);
    self.targetFinishLabel.text = NSLocalizedString(@"To achieve your goals, you will", nil);
    self.totalExerciseDescriptionLabel.text = NSLocalizedString(@"Exercise for", nil);
    self.totalCaloriesDescriptionLabel.text = NSLocalizedString(@"Burned", nil);
    self.minuteUnitLabel.text = NSLocalizedString(@"Unit.Minute", nil);
    self.kcalUnitLabel.text = NSLocalizedString(@"Unit.Kcal", nil);
    self.swingUnitLabel.text = NSLocalizedString(@"Unit.Shots", nil);
}

#pragma mark - Nav

- (void)configureNav
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_save"] style:UIBarButtonItemStylePlain target:self action:@selector(uploadTarget)];
}

- (void)back
{
    FUIAlertView *alert = [[FUIAlertView alloc] initWithTitle:NSLocalizedString(@"Settings have been changed. Do you want to save this target settings？", nil) message:@"" delegate:nil cancelButtonTitle:NSLocalizedString(@"No", nil) otherButtonTitles:NSLocalizedString(@"Target.Yes", nil), nil];
    [alert customizeAppearance];
    alert.onOkAction = ^{
        NSLog(@"保存上传运动目标");
        [self uploadTarget];
    };
    alert.onCancelAction = ^{
        NSLog(@"取消目标更改");
        [self.navigationController popViewControllerAnimated:YES];
    };
    
    if (self.currentTargetSwings != self.hasSettingTargetSwings) {
        [alert show];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (void)uploadTarget
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //上传目标
    YFStatisticsApi *setTargetApi = [YFStatisticsApi changeStatisticsTargetWithCount:self.currentTargetSwings];
    
    [[YFNetworkingService sharedInstance] sendRequest:setTargetApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            NSLog(@"上传目标成功");
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self addCoverView];
            self.targetCompleBlock(self.currentTargetSwings);
            [MBProgressHUD showSuccessHudInView:self.view message:NSLocalizedString(@"Successfully Saved", nil) afterDelay:1];
            [self performSelector:@selector(popNav) withObject:nil afterDelay:1];
        }
    }];
}

- (void)popNav
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.coverView removeFromSuperview];
}

- (void)addCoverView
{
    self.coverView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
}

#pragma mark - IBAction
- (IBAction)changeSliderValue:(id)sender {
    
    self.currentTargetSwings = (NSInteger)floor(self.slider.value * 100 / UnitSpace) *50 + 50;
    
    self.weekTargetSwingLabel.text = [NSString stringWithFormat:@"%zd",self.currentTargetSwings];
    self.totalSportTimeLabel.text = [NSString stringWithFormat:@"%zd",self.currentTargetSwings / 6];
    self.totalCaloriosLabel.text = [NSString stringWithFormat:@"%zd",self.currentTargetSwings * 7 /6];
}

#pragma mark - Private Method
- (void)configureProgressView
{
    self.slider.font = [UIFont systemFontOfSize:12];
    [self.slider showPopUpViewAnimated:YES];
    self.slider.popUpViewAnimatedColors = @[[UIColor colorWithHex:0x4296fe alpha:1.0],
                                                  [UIColor colorWithHex:0x25c9ad alpha:1.0],
                                                  [UIColor colorWithHex:0xec6941 alpha:1.0],
                                                  [UIColor colorWithHex:0xe60012 alpha:1.0]];
    self.slider.dataSource = self;
    [self.slider setThumbImage:[UIImage imageNamed:@"sport_icon_target_ball"] forState:UIControlStateNormal];
    self.slider.popUpViewCornerRadius = 12;
    [self.slider hidePopUpViewAnimated:YES];
}

- (void)configureCountingLabel
{
    self.weekTargetSwingLabel.format = @"%d";
    self.totalSportTimeLabel.format = @"%d";
    self.totalCaloriosLabel.format = @"%d";
}

- (void)configureHasSettingTargetSwings
{
    self.weekTargetSwingLabel.text = [NSString stringWithFormat:@"%zd",self.hasSettingTargetSwings];
    self.totalSportTimeLabel.text = [NSString stringWithFormat:@"%zd",self.hasSettingTargetSwings / 6];
    self.totalCaloriosLabel.text = [NSString stringWithFormat:@"%zd",self.hasSettingTargetSwings * 7 /6];
    self.currentTargetSwings = self.hasSettingTargetSwings;
    self.slider.value = (self.currentTargetSwings - 50)/50.00 * UnitSpace /100;
    
    CGFloat margin = 12.0f;
    self.averageLineValueConstraint.constant = (CGFloat)self.averageTargetSwings/(TotalSwingTarget) * ([UIScreen mainScreen].bounds.size.width - 2 * margin);
    self.recommendLineValueConstraint.constant = (CGFloat)self.recommendTargetSwings/(TotalSwingTarget) * ([UIScreen mainScreen].bounds.size.width - 2 * margin);
}

#pragma mark - ASValueTrackingSliderDataSource

- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value
{
    return [NSString stringWithFormat:@"%ld  ",(long)self.currentTargetSwings];
}

@end
